from pydantic import BaseModel, Field
from typing import List, Optional
from datetime import datetime

class ParameterValueCreate(BaseModel):
    time_point: datetime = Field(..., description="时间点")
    value: float = Field(..., description="参数数值")

class ParameterCreate(BaseModel):
    category_id: int = Field(..., description="参数类别ID")
    period_unit: str = Field(..., description="划分周期单位")
    period: int = Field(..., description="划分周期")
    granularity: int = Field(..., description="划分粒度")
    parameter_name: str = Field(..., description="参数名称")
    icon: Optional[str] = Field("", description="图标")
    background: Optional[str] = Field("", description="背景色")
    values: List[ParameterValueCreate] = Field(..., description="参数值列表，数量应等于粒度")

class ParameterCreateList(BaseModel):
    category_id: int = Field(..., description="参数类别ID")
    period_unit: str = Field(..., description="划分周期单位")
    period: int = Field(..., description="划分周期")
    granularity: int = Field(..., description="划分粒度")
    parameter_name: str = Field(..., description="参数名称")
    icon: Optional[str] = Field("", description="图标")
    background: Optional[str] = Field("", description="背景色")
    values: List[float] = Field(..., description="参数值列表，数量应等于粒度")

class ParameterOut(BaseModel):
    parameter_id: int
    category_id: int
    period_unit: str = Field(..., description="周期单位，例如 'hour'")
    period: int = Field(..., description="周期")
    granularity: int = Field(..., description="粒度，例如一天分 24 段")
    parameter_name: str = Field(..., description="参数名称")
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空

    class Config:
        from_attributes = True


class ParameterDetailOut(BaseModel):
    parameter_id: int
    period_unit: str
    period: int
    granularity: int
    parameter_name: str
    category_id: int
    time_list: List[datetime]
    value_list: List[float]
    icon: Optional[str] = None  # 允许为空
    background: Optional[str] = None  # 允许为空

    class Config:
        from_attributes = True